home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / management / priman_1.0 / priman.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  51KB  |  1,077 lines

  1. /*
  2.  
  3.     Task Priority Manager
  4.     Copyright 1993 Barry McConnell
  5.     bmccnnll@unix1.tcd.ie
  6.  
  7. */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <exec/execbase.h>
  12. #include <dos/dos.h>
  13. #include <dos/dosextens.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <intuition/screens.h>
  17. #include <graphics/text.h> 
  18. #include <graphics/gfxbase.h>
  19. #include <libraries/gadtools.h>
  20. #include <libraries/asl.h>
  21. #include <workbench/startup.h>
  22. #include <workbench/workbench.h>
  23.  
  24. #include <proto/dos.h>
  25. #include <proto/exec.h>
  26. #include <proto/intuition.h>
  27. #include <proto/graphics.h>
  28. #include <proto/gadtools.h>
  29. #include <proto/diskfont.h>
  30. #include <proto/asl.h>
  31. #include <proto/icon.h>
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. #define WINDOWIDCMP     IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | IDCMP_NEWSIZE | BUTTONIDCMP | LISTVIEWIDCMP | RAWKEY
  37. #define DefFontLength   15  /* including the size */
  38. #define MaxFontLength   30  /* well, have YOU seen any font names longer than this?? */
  39. #define ScrollBarWidth  16
  40.  
  41. #define LISTGAD         1
  42. #define SLIDERGAD       2
  43. #define BREAKGAD        3
  44. #define KILLGAD         4
  45. #define SETTINGSGAD     5
  46. #define GFONTGAD        6
  47. #define LFONTGAD        7
  48. #define GBOXGAD         8
  49. #define LBOXGAD         9
  50. #define CHECKGAD        10
  51. #define SAVEGAD         11
  52. #define USEGAD          12
  53. #define CANCELGAD       13
  54.  
  55. void __regargs __chkabort(void) {}  /* turn off SAS/C v6 CTRL-C checking */
  56.  
  57. int showTasks(void);
  58. int handleSettings(struct Screen *, struct Window *,
  59.                     struct Gadget *, struct Gadget *, struct Gadget *, struct Gadget *,
  60.                     WORD, WORD, WORD);
  61.  
  62. struct listType  /* standard Node type with extra space to store the original task structure, so we can find it again */
  63.     {
  64.     struct Node mainNode;
  65.     struct Task *mainTask;
  66.     };
  67.  
  68. struct FontRequester    *propFontReq = NULL, *monoFontReq = NULL;
  69. struct TextAttr         propName = { "                         ", 0, 0, 0 },
  70.                         monoName = { "                         ", 0, 0, 0 };
  71.  
  72. extern struct ExecBase *SysBase;
  73. static UBYTE *version = "$VER: PriMan 1.0 (03.09.93)";
  74. char *myName = NULL;  /* to be filled in from WBStartup message */
  75. int workbench = 0;  /* started from CLI or Workbench? */
  76. UWORD osver = 0;  /* Kickstart version */
  77. WORD winLeft = 150, winTop = 50, winWidth = 220, winHeight = 270, confirm = 1;  /* default settings */
  78.  
  79. UWORD __chip waitPointer[] =
  80.     {
  81.     0x0000, 0x0000,
  82.     0x0400, 0x07c0,
  83.     0x0000, 0x07c0,
  84.     0x0100, 0x0380,
  85.     0x0000, 0x07e0,
  86.     0x07c0, 0x1ff8,
  87.     0x1ff0, 0x3fec,
  88.     0x3ff8, 0x7fde,
  89.     0x3ff8, 0x7fbe,
  90.     0x7ffc, 0xff7f,
  91.     0x7efc, 0xffff,
  92.     0x7ffc, 0xffff,
  93.     0x3ff8, 0x7ffe,
  94.     0x3ff8, 0x7ffe,
  95.     0x1ff0, 0x3ffc,
  96.     0x07c0, 0x1ff8,
  97.     0x0000, 0x07e0,
  98.     0x0000, 0x0000,
  99.     };
  100.  
  101.  
  102. /*
  103.     Display a busy pointer in the current window, and prevent user from
  104.     clicking on any gadgets. The immediate flag prevents the short delay
  105.     (under 3.x) before the pointer changes.
  106. */
  107.  
  108. void busyPointer(struct Window *myWindow, struct Requester *myReq, BOOL immediate)
  109. {
  110. InitRequester(myReq);
  111. Request(myReq, myWindow);
  112. if (osver < 39 || immediate)
  113.     SetPointer(myWindow, waitPointer, 16, 16, -6, 0);
  114. else
  115.     SetWindowPointer(myWindow,  WA_BusyPointer,     TRUE,
  116.                                 WA_PointerDelay,    TRUE,
  117.                                 TAG_END);
  118. }
  119.  
  120.  
  121. /*
  122.     Restore the pointer to normal. The keepPointer flag just removes the invisible
  123.     requester without restoring the normal pointer image.
  124. */
  125.  
  126. void normalPointer(struct Window *myWindow, struct Requester *myReq, BOOL keepPointer)
  127. {
  128. if (!keepPointer)
  129.     ClearPointer(myWindow);
  130. EndRequest(myReq, myWindow);
  131. }
  132.  
  133.  
  134. /*
  135.     Join a text string (task or font name) and a number (priority or size) together.
  136.  
  137.     If it's a task, extra spaces are placed between the two components to make the
  138.     string an exact length. If the task has an appropriate CLI number, that will be
  139.     added in before the task name.
  140.  
  141.     If it's a font, the routine stops at a dot (i.e. in "x.font"), and places a
  142.     single space between it and the size.
  143.  
  144.     There must be length+1 bytes free in dest, because of the \0 char.
  145. */
  146.  
  147. void createString(struct Task *task, struct TextAttr *font, char *dest, int length)
  148. {
  149. int numLen, cli, i = 0, j = 0;
  150. char digits[5], clitext[11];
  151. char *name;
  152. APTR bstr;
  153. BYTE maxlen = -1;
  154.  
  155. if (task)  /* dealing with a task or a font? */
  156.     {
  157.     numLen = sprintf(digits, "%d", task -> tc_Node.ln_Pri);  /* saves me writing an integer -> ASCII routine */
  158.  
  159.     if (((task -> tc_Node.ln_Type) == NT_PROCESS) && (cli = ((struct Process *)task) -> pr_TaskNum))  /* is it a process with a CLI attached? */
  160.         {
  161.         sprintf(clitext, "CLI #%d: ", cli);
  162.         for (; (i < length - numLen - 1) && clitext[i]; i++)
  163.             dest[i] = clitext[i];
  164.         bstr = BADDR(((struct CommandLineInterface *)(BADDR(((struct Process *)task) -> pr_CLI))) -> cli_CommandName);
  165.         name = (char *)(bstr)+1;
  166.         maxlen = *((BYTE *)bstr);
  167.         }
  168.     else
  169.         name = task -> tc_Node.ln_Name;
  170.     }
  171.  
  172. else  /* font */
  173.     {
  174.     numLen = sprintf(digits, "%d", font -> ta_YSize);
  175.     name = font -> ta_Name;
  176.     }
  177.  
  178. for (; (i < length - numLen - 1) && name[j] && maxlen && !((name[j] == '.') && font); i++, j++, maxlen--)
  179.     dest[i] = name[j];
  180.  
  181. do
  182.     dest[i++] = ' ';
  183. while ((i < length - numLen) && task);  /* only insert one space if it's a font name */
  184.  
  185. j = 0;
  186. do
  187.     dest[i++] = digits[j];
  188. while (digits[j++]);
  189. }
  190.  
  191.  
  192. /*
  193.     Compare two strings. Like "strcmp" but case-insensitive.
  194. */
  195.  
  196. int Compare(char *s, char *t)
  197. {
  198. for (; ((*s & ~32) == (*t & ~32)) && *s; s++, t++)  /* sorry, K&R ;-) */
  199.     ;
  200. return (*s & ~32) - (*t & ~32);
  201. }
  202.  
  203.  
  204. /*
  205.     Alphabetically insert "node" into "list".
  206. */
  207.  
  208. void Sort(struct List *list, struct Node *node)
  209. {
  210. struct Node *temp = list -> lh_Head;
  211.  
  212. while ((temp -> ln_Succ) && Compare(temp -> ln_Name, node -> ln_Name) < 0)
  213.     temp = temp -> ln_Succ;
  214.  
  215. Insert(list, node, temp -> ln_Pred);
  216. }
  217.  
  218.  
  219. /*
  220.     Add an Exec task list ("source") onto our own one ("dest").
  221.     Allocates memory as necessary, and preserves alphabetical order.
  222.     If we just want to add one task, use the last parameter ("single") -
  223.     in this case, "source" will be ignored.
  224. */
  225.  
  226. int BuildList(struct List *source, struct List *dest, struct Remember **memoryKey, int taskLength, struct Task *single)
  227. {
  228. struct Node     *execNode;
  229. struct listType *taskNode;
  230. int             success = TRUE;
  231. UBYTE           *taskString;
  232.  
  233. if (single)
  234.     {
  235.     if ((taskNode = AllocRemember(memoryKey, sizeof(struct listType), MEMF_ANY))
  236.         && (taskString = AllocRemember(memoryKey, taskLength + 1, MEMF_ANY)))
  237.         {
  238.         createString(single, NULL, taskString, taskLength);
  239.         taskNode -> mainNode.ln_Name = taskString;
  240.         taskNode -> mainNode.ln_Pri = single -> tc_Node.ln_Pri;
  241.         taskNode -> mainTask = single;
  242.         Sort(dest, (struct Node *)taskNode);
  243.         }
  244.     else
  245.         success = FALSE;
  246.     }
  247. else
  248.     {   
  249.     for (execNode = source -> lh_Head; (execNode -> ln_Succ) && success; execNode = execNode -> ln_Succ)
  250.         {
  251.         if ((taskNode = AllocRemember(memoryKey, sizeof(struct listType), MEMF_ANY))
  252.             && (taskString = AllocRemember(memoryKey, taskLength + 1, MEMF_ANY)))
  253.             {
  254.             createString((struct Task *)execNode, NULL, taskString, taskLength);
  255.             taskNode -> mainNode.ln_Name = taskString;
  256.             taskNode -> mainNode.ln_Pri = execNode -> ln_Pri;
  257.             taskNode -> mainTask = (struct Task *)execNode;
  258.             Sort(dest, (struct Node *)taskNode);
  259.             }
  260.         else
  261.             success = FALSE;
  262.         }
  263.     }
  264. return success;
  265. }
  266.  
  267.  
  268. /*
  269.     Search for given ToolType in array.
  270.     Fills in string -> decimal value if found.
  271. */
  272.  
  273. void match(char *tool, char **array, WORD *value)
  274. {
  275. char    *string;
  276. int     temp;
  277.  
  278. if (string = FindToolType(array, tool))  /* is it there? */
  279.     {
  280.     sscanf(string, "%d", &temp);  /* read it into type "int" */
  281.     *value = temp;  /* convert to "word" */
  282.     }
  283. }
  284.  
  285.  
  286. void main(int argc, char *argv[])
  287. {
  288. struct DiskObject   *myIcon;
  289. char                **tools, *s